package com.huike.web.controller.business;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.service.ITbBusinessService;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;

/**
 * 商机跟进记录Controller
 * @date 2021-04-28
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController extends BaseController {

    @Autowired
    private ITbBusinessTrackRecordService tbBusinessTrackRecordService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    /**
     * 查询商机跟进记录列表(第四天任务)
     */
    @PreAuthorize("@ss.hasPermi('business:record:list')")
    @GetMapping("/list")
    public  AjaxResult list(@RequestParam("businessId")Long id){
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordService.selectTbBusinessTrackRecordListByBusinessId(id);
        for (TbBusinessTrackRecord businessTrackRecord : list) {  //由于一个商机可能对应于多个商机跟进记录，所以遍历商机跟进记录集合
            String[] items = businessTrackRecord.getKeyItems().split(",");  //因为对象中存储的沟通重点并不是直接以字符串的形式存入的，而是存入的是它对应的键值，因此需要先将该键值组成的字符串分隔变成数组以便于后续处理
            //List<String> items = Stream.of(item_s).filter(x -> x != null).collect(Collectors.toList());
            for (String item : items) {     //遍历沟通对象属性对应的键值数组
                String dictLable = sysDictDataService.selectDictLabel("communication_point", item);  //调用sysDictDataService对象的方法查找对应键值对应的字典标签(dict_label)
                businessTrackRecord.getKeys().add(dictLable);  //每获取到一个字典标签时就将它加入到businessTrackRecord对象的keys数组中
            }
        }
        return AjaxResult.success(list);
    }

    /**
     * 新增商机跟进记录(第四天任务)
     */
    @PreAuthorize("@ss.hasPermi('business:record:add')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTrackVo businessTrackVo){
        //1.构建根据记录对象
        TbBusinessTrackRecord trackRecord = new TbBusinessTrackRecord();
        //2.封装跟进记录对象属性，除开基本属性外设置createTime，设置创建人creatBy
        BeanUtils.copyProperties(businessTrackVo,trackRecord);
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        //3.构建商机对象
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo,tbBusiness);
        //4.设置商机的状态为跟进中
        tbBusiness.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        //5.根据商机ID修改商机
        tbBusiness.setId(businessTrackVo.getBusinessId());
        //6.添加商机跟进记录
        return toAjax(tbBusinessTrackRecordService.insertTbBusinessTrackRecord(tbBusiness,trackRecord));
    }

}